package command;

import java.net.InetAddress;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import base.*;

public class AnnounceCommand extends Command {

   private InetAddress ip;
   private int port;

   private List<File> seedFiles;
   private List<String> leechKeys;
   private String sresponse;

   public AnnounceCommand(){
      seedFiles = new LinkedList<File>();
      leechKeys = new LinkedList<String>(); 
   }


   public void addSeedFile(File f){
      seedFiles.add(f);
   }
   
   public void addLeechKey(String k){
      leechKeys.add(k);
   }

   public boolean run() {
      
      Peer p = new Peer(this.ip, this.port);
      DataTracker d = new DataTracker();
      d.createEm();
      
      // Ajout des fichiers appartenant au peer
      
      
       for(File f: seedFiles)   	  
        d.addSeeder(f,p);
      
      for(String tmp : leechKeys)
    	 d.addLeecher(tmp, p);
      
      	//d.printFiles();
      	//d.printPeers();
      	//d.printShares();
      	d.closeEm();
      	this.sresponse  = "OK";
      	return true;
   }

   /**
    * Sets the ip for this instance.
    *
    * @param ip The ip.
    */
   public void setIp(InetAddress ip)
   {
      this.ip = ip;
   }

   /**
    * Sets the port for this instance.
    *
    * @param port The port.
    */
   public void setPort(int port)
   {
      this.port = port;
   }


   public String getSresponse() 
   {
	   return sresponse;
   }
   
   public String toString(){
	   return "ip:"+ ip +" port:"+port+" seed:"+ seedFiles+" leech:"+leechKeys ;
   }
   

}
